//
//  ImageLodingView.m
//  YeeShopProject
//
//  Created by CoderYee on 2017/3/14.
//  Copyright © 2017年 CoderYee. All rights reserved.
//

#import "ImageLodingView.h"

@implementation ImageLodingView
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame])
    {
        [self initLoadingView:self];

    }
    return self;
}
-(void)initLoadingView: (UIView*)view
{
    
    CGRect rect = view.frame;
    
    if (_loadingBackground == nil) {
        _loadingBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 145, 145)];
        _loadingBackground.hidden = YES;
        _loadingBackground.backgroundColor = [UIColor blackColor];
        _loadingBackground.alpha  = 0.5;
        _loadingBackground.layer.masksToBounds=YES;
        _loadingImageView.layer.cornerRadius=5.0f;
        _loadingBackground.center=view.center;
        [view addSubview:_loadingBackground];
//        
//        UITextView * textView = [[UITextView alloc]init];
//        textView.bounds = CGRectMake(0, 0, CGRectGetWidth(rect), 30);
//        textView.center = CGPointMake(CGRectGetWidth(rect) / 2, CGRectGetHeight(rect) / 2 - 30);
//        textView.textAlignment = NSTextAlignmentCenter;
//        textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
//        textView.textColor = [UIColor blackColor];
//        textView.text = @"连麦中···";
//         textView.hidden = YES;
//        [_loadingBackground addSubview:textView];
    }
    
    if (_loadingImageView == nil) {
        float width = 50;
        float height = 50;
        NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"loading_image0.png"],
                                 [UIImage imageNamed:@"loading_image1.png"],
                                 [UIImage imageNamed:@"loading_image2.png"],
                                 [UIImage imageNamed:@"loading_image3.png"],
                                 [UIImage imageNamed:@"loading_image4.png"],
                                 [UIImage imageNamed:@"loading_image5.png"],
                                 [UIImage imageNamed:@"loading_image6.png"],
                                 [UIImage imageNamed:@"loading_image7.png"],
                                 [UIImage imageNamed:@"loading_image8.png"],
                                 [UIImage imageNamed:@"loading_image9.png"],
                                 [UIImage imageNamed:@"loading_image10.png"],
                                 [UIImage imageNamed:@"loading_image11.png"],
                                 [UIImage imageNamed:@"loading_image12.png"],
                                 [UIImage imageNamed:@"loading_image13.png"],
                                 [UIImage imageNamed:@"loading_image14.png"],
                                 nil];
        _loadingImageView = [[UIImageView alloc] init];
        _loadingImageView.bounds = CGRectMake(0, 0, width, height);
        _loadingImageView.center = CGPointMake(CGRectGetWidth(rect) / 2, CGRectGetHeight(rect) / 2);
        _loadingImageView.animationImages = array;
        _loadingImageView.animationDuration = 1;
        _loadingImageView.hidden = YES;
        [view addSubview:_loadingImageView];
    }

}
-(void)startLoading {
    if (_loadingBackground) {
        _loadingBackground.hidden = NO;
    }
    
    if (_loadingImageView) {
        _loadingImageView.hidden = NO;
        [_loadingImageView startAnimating];
    }
}

-(void)stopLoading {
    if (_loadingBackground) {
        _loadingBackground.hidden = YES;
    }
    
    if (_loadingImageView) {
        _loadingImageView.hidden = YES;
        [_loadingImageView stopAnimating];
    }
}
//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
//{
//    UIView *hitView = [super hitTest:point withEvent:event];
//    if (hitView == self)
//    {
//        return nil;
//    } else
//    {
//        return hitView;
//    }
//}
- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event{
    
    return NO;
}

@end
